﻿using System;
using NUnit.Framework;
using OpenQA.Selenium;

namespace mvcForum.Tests.WebUI {

	[TestFixture]
	public class MergeTests {
		private static IWebDriver driver;
		private String forumName;

		#region Initialization and cleanup
		[TestFixtureSetUp]
		public void TestInitialize() {
			driver = TestHelper.GetDriver();

			// Log on as administrator!
			driver.LogOn(Constants.Users.Admin.Email, Constants.Users.Admin.Password);

			forumName = Guid.NewGuid().ToString();
			driver.CreateForum("Test board", "Category 1", forumName, "", 1);
			driver.SetStandardAccess("Test board", "Category 1", forumName);

			// Disable multi-post anti-spam!
			driver.DisableAntiSpam();

			driver.LogOff();
		}

		[TestFixtureTearDown]
		public void TestCleanup() {
			if (driver != null) {
				driver.Close();
			}
		}
		#endregion

		[Test]
		public void ASimpleMergeTest() {
			driver.LogOn(Constants.Users.Regular.Email, Constants.Users.Regular.Password);

			String topic1 = "The first topic";
			String phrase1 = "wonker";
			driver.CreateTopic(forumName, topic1, "Bla bla bla, the first topic, should end up at the top " + phrase1);

			String topic2 = "The second topic";
			String phrase2 = "willy";
			driver.CreateTopic(forumName, topic2, "Bla bla bla, the second topic, should end up at the bottom " + phrase2);

			Int32 topics, posts;
			String latestTopic, latestAuthor;
			driver.VisitURL("forum");
			driver.GetForumInfo(forumName, out topics, out posts, out latestTopic, out latestAuthor);

			Assert.AreEqual(2, topics, "2 topics posted, but not 2 found!");
			Assert.AreEqual(0, posts, "No posts, but posts found!");

			driver.MergeTopics(forumName, topic1, topic2);

			driver.VisitURL("forum");
			driver.GetForumInfo(forumName, out topics, out posts, out latestTopic, out latestAuthor);

			Assert.AreEqual(1, topics, "1 topic after merge, but not 1 found!");
			Assert.AreEqual(1, posts, "1 post, but more/less posts found!");

			driver.Wait(2 * 15);

			driver.Search(phrase1, forumName);

			driver.Search(phrase2, forumName);
			// TODO: Test topic 1 at the top, topic 2 is now a post!
		}
	}
}